/*
	system_config_table.c

	contains functions for setting and retrieving system configuration information

	Author: Aidan Goddard 16/5/13
*/

#include"headers/types.h"
#include"headers/kernel_tables.h"
#include"headers/system_config_tables.h"
#include"headers/scheduler_functions.h"
#include"headers/bakery_lock.h"
#include"../setup/headers/printf.h"

// the tables global lock
static SCH_CLS table_lock;

// functions to aquire and release the table lock
// need to be called with interrupts disabled
uint32_t SystemTableLock()
{
	int CPU = GetCPUID();
	BakeryEnter(&table_lock, CPU);
	return CPU;
}

void SystemTableUnlock(int CPU)
{
	BakeryLeave(&table_lock, CPU);
	return;
}

// function for getting a pointer to the base system config table
// pointer is fixed at [k + 14MB] = 0x100e00000
TBL_SYSCONFIG *GetSystemConfigBase()
{
	// DEBUG: print table sizes to make sure they are correct
	/*
	printf("\n%u 0x%x", sizeof(TBL_SYSCONFIG), sizeof(TBL_SYSCONFIG));
	printf("\n%u", sizeof(TBL_SYSCONFIG_STUFF));
	printf("\n%u", sizeof(TBL_SYSCONFIG_ISA_GSI));
	printf("\n%u", sizeof(TBL_SYSCONFIG_GSI));
	printf("\n%u", sizeof(TBL_SYSCONFIG_IOAPIC));
	printf("\n%u", sizeof(TBL_SYSCONFIG_CPU));
	*/
	return (TBL_SYSCONFIG*)(0xe00000 + KOFFSET);
}


// function for setting the APIC bus speed
void SetAPICTimerBusSpeed(uint32_t ticks_per_second)
{
	GetSystemConfigBase()->tbl_stuff.APIC_ticks_sec = ticks_per_second;
	return;
}

// function for getting the APIC bus speed
uint32_t GetAPICTimerBusSpeed()
{
	return GetSystemConfigBase()->tbl_stuff.APIC_ticks_sec;
}
